<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        id: {
          hidden: true
        },
        type: {
          label: '入库类别',
          type: 'radio',
          radioBorder: false,
          radioList: [
            {label: 3, text: '生产使用'},
            {label: 0, text: '采购'},
            {label: 1, text: '返回模具厂'},
            {label: 2, text: '外调'},
            {label: 4, text: '客户提供'},
            {label: 5, text: '从客户拉回'},
          ],
          default: 0
        },
        contractPurchaseId: {
          label: '采购合同号',
          type: 'list',
          list: [],
          reply: () => this.R.equals(0, this.dataForm['type']),
          placeholder: '请选择模具采购合同号',
          selectCallback: this.contractPurchaseHandle
        },
        purchaseModelId: {
          label: '采购模具信息',
          type: 'list',
          list: [],
          placeholder: '请选择模具采购列表',
          reply: () => this.R.equals(0, this.dataForm['type']),
          selectCallback: this.purchaseModelHandle
        },
        //modelInfoId
        modelOutId: {
          label: '模具出库记录',
          type: 'list',
          list: [],
          placeholder: '请选择模具出库记录',
          reply: () => this.R.equals(5, this.dataForm['type']),
          selectCallback: this.modelOutHandle
        },
        modelInfoId: {
          label: '模具',
          type: 'list',
          list: [],
          placeholder: '请选择模具',
          isNeedAdd: true,
          addNewItemFunc: this.addNewModel,
          selectCallback: this.modelSelectedHandle
        },
        modelShelfId: {
          label: '架号',
          type: 'list',
          filterable: true,
          list: [],
          placeholder: '请输入架号'
        },
        modelType: {
          label: '生产/试用用模',
          type: 'radio',
          radioBorder: false,
          radioList: [
            {text: '试样模', label: 0},
            {text: '生产模', label: 1}
          ],
          default: 1
        },
        modelCategoryId: {
          label: '模具类别',
          type: 'list',
          placeholder: '请选择模具类别',
          list: []
        },
        modelCalibre: {
          label: '模具口径',
        },
        productWeight: {
          label: '克重',
        },
        successMo: {
          label: '成模数量',
          type: 'number',
          max: 1000,
          //selectCallback: this.successMoChangedHandle,
          default: 0
        },
        primaryMo: {
          label: '初模数量',
          type: 'number',
          max: 1000,
          default: 0
        },
        menTou: {
          label: '闷头数量',
          type: 'number',
          max: 1000,
          default: 0
        },
        louDou: {
          label: '漏斗数量',
          type: 'number',
          max: 1000,
          default: 0
        },
        kouZi: {
          label: '口子数量',
          type: 'number',
          max: 1000,
          default: 0
        },
        xinZi: {
          label: '芯子数量',
          type: 'number',
          max: 1000,
          default: 0
        },
        qiTou: {
          label: '气头数量',
          type: 'number',
          max: 1000,
          default: 0
        },
        // jiaZi: {
        //   label: '夹子数量',
        //   type: 'number',
        //   max: 1000,
        //   default: 0
        // },
        qianPian: {
          label: '钳片数量',
          type: 'number',
          max: 1000,
          default: 0
        },
        lengQue: {
          label: '冷却数量',
          type: 'number',
          max: 1000,
          default: 0
        },
        clientId: {
          label: '客户名称',
          type: 'list',
          placeholder: '请输入客户名称',
          list: []
        },
        dayTime: {
          label: '日期',
          type: 'date',
          placeholder: '请选择日期'
        },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      }
    }
  },
  methods: {
    addNewModel() {
      const cb = (newValue) => {
        this.initModelInfoList()
        this.dataForm.modelInfoId = newValue.id
      }
      this.$parent.openModelInfoDialog(cb)
    },
    modelSelectedHandle() {
      mRequest('modelApi/modelInfoInfo', {id: this.dataForm.modelInfoId}, ({data}) => {
        const {modelInfo} = data

        if (this.R.isNil(modelInfo)) {
          return
        }
        this.dataForm.clientId = this.R.propOr(0, 'clientId', modelInfo)
        this.dataForm.modelShelfId = this.R.propOr(0, 'shelfId', modelInfo)
      })
    },
    successMoChangedHandle() {
      this.dataForm.successMo = this.dataForm.successMo || 0

      this.dataForm.primaryMo = this.dataForm.successMo
      this.dataForm.jiaZi = this.dataForm.successMo
      this.dataForm.kouZi = this.dataForm.successMo
      this.dataForm.lengQue = this.dataForm.successMo
      this.dataForm.louDou = this.dataForm.successMo
      this.dataForm.menTou = this.dataForm.successMo
      this.dataForm.qiTou = this.dataForm.successMo
      this.dataForm.xinZi = this.dataForm.successMo
      this.dataForm.qianPian = this.dataForm.successMo
    },
    contractPurchaseHandle() {
      if (this.R.isNil(this.dataForm.contractPurchaseId)) {
        return
      }
      mRequest('purchaseApi/getPurchaseModelListByContractId', {contractPurchaseId: this.dataForm.contractPurchaseId}, ({data}) => {

        this.dataFormConfig['purchaseModelId'].list = this.R.pathOr([], ['list'], data)
      })
    },
    purchaseModelHandle() {
      mRequest('purchaseApi/purchaseModelInfo', {id: this.dataForm.purchaseModelId}, ({data}) => {
        const {purchaseModel} = data
        this.dataForm.modelInfoId = this.R.propOr(0, 'modelId', purchaseModel)
        this.dataForm.successMo = this.R.propOr(0, 'successMo', purchaseModel)
        this.dataForm.primaryMo = this.R.propOr(0, 'primaryMo', purchaseModel)
        this.dataForm.menTou = this.R.propOr(0, 'menTou', purchaseModel)
        this.dataForm.louDou = this.R.propOr(0, 'louDou', purchaseModel)
        this.dataForm.kouZi = this.R.propOr(0, 'kouZi', purchaseModel)
        this.dataForm.xinZi = this.R.propOr(0, 'xinZi', purchaseModel)
        this.dataForm.qiTou = this.R.propOr(0, 'qiTou', purchaseModel)
        this.dataForm.jiaZi = this.R.propOr(0, 'jiaZi', purchaseModel)
        this.dataForm.qianPian = this.R.propOr(0, 'qianPian', purchaseModel)
        this.modelSelectedHandle()
      })
    },
    modelOutHandle() {
      mRequest('modelApi/modelOutInfo', {id: this.dataForm.modelOutId}, ({data}) => {
        const {modelOut} = data
        this.dataForm.modelInfoId = this.R.propOr(0, 'modelInfoId', modelOut)
        this.dataForm.modelShelfId = this.R.propOr(0, 'modelShelfId', modelOut)
        this.dataFormConfig['successMo']['max']= this.R.propOr(0, 'successMo', modelOut)
        this.dataFormConfig['primaryMo']['max'] = this.R.propOr(0, 'primaryMo', modelOut)
      })
    },
    init(id) {
      this.dataForm.id = id || 0
      this.initModelInfoList()
      this.initModelOutList()
      this.initShelfList()
      this.initModelCategoryList()
      this.initClientInfoList('clientId')
      this.initContractPurchaseModelInfoList()
      this.dataFormConfig['purchaseModelId'].list = []
      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule('modelInfoId', 'type','modelShelfId','dayTime', 'clientName', 'status'),
        initApi: 'modelApi/modelAddInfo',
        saveApi: 'modelApi/saveModelAdd',
        updateApi: 'modelApi/updateModelAdd',
        objectName: 'modelAdd'
      })
    }
  }
}
</script>
